/**
 * 
 */
package com.skood.security.action;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.apache.struts2.json.JSONUtil;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;

import com.opensymphony.xwork2.ActionContext;
import com.skood.common.ExecuteResult;
import com.skood.platform.webapp.action.BaseAction;
import com.skood.security.domain.User;
import com.skood.security.model.UserSearchModel;
import com.skood.security.service.UserService;

/**
 * @author keege
 * @date 2013-10-11
 */
@SuppressWarnings("serial")
@Controller("userAction")
@Scope("prototype")
public class UserAction extends BaseAction<UserSearchModel,User>{
	/**************************
	 * members
	 **************************/
	//@Autowired
	//@Qualifier("userService")//@Autowired根据类型自动匹配,使用@Qualifier配合@Autowired指定bean的name
	//建议使用@Resource
	@Resource(name="userService")//@Resource有两个参数name、type指定哪个参数就会按照哪个参数匹配，如果不指定先按照name匹配，再按照type自动匹配
	private UserService userService;
	
	private User user;
	
	private Long id;
	/**************************
	 * methods
	 **************************/
	public String execute()throws Exception{
		return SUCCESS;
	}
	public String doJsonUserList()throws Exception{
		if(searchModel.getSort()!=null){
			Object sorters = JSONUtil.deserialize(searchModel.getSort());
			searchModel.setSorters(sorters);
		}
		List<User> users = userService.searchUsers(searchModel);
		Long total = userService.searchUserCount(searchModel);
		Map<String,Object> resultMap = new HashMap<String,Object>();
		resultMap.put("success", true);
		resultMap.put("rows", users);
		resultMap.put("total", total);
		response().setCharacterEncoding("UTF-8");
		String jsonObj = JSONUtil.serialize(resultMap);
		response().getWriter().write(jsonObj);
		
		return null;
	}
	public String doCreate()throws Exception{
		userService.createUser(entityBean);
		Map<String,Object> resultMap = new HashMap<String,Object>();
		resultMap.put("success", true);
		response().setCharacterEncoding("UTF-8");
		String jsonObj = JSONUtil.serialize(resultMap);
		response().getWriter().write(jsonObj);
		
		return null;
	}
	
	public String doRemove()throws Exception{
		userService.removeUser(id);
		Map<String,Object> resultMap = new HashMap<String,Object>();
		resultMap.put("success", true);
		response().setCharacterEncoding("UTF-8");
		String jsonObj = JSONUtil.serialize(resultMap);
		response().getWriter().write(jsonObj);
		return null;
	}
	/**
	 * 用户登录
	 */
	public String login() throws Exception{
		ExecuteResult<User> executeResult = userService.login(user.getName(), user.getPassword());
		if(!executeResult.isSuccess()){
			return INPUT;
		}
		ActionContext.getContext().getSession().put("_current_user", executeResult.getResult());
		return SUCCESS;
	}
	/**
	 * 用户登出
	 */
	public String logout()throws Exception{
		return SUCCESS;
	}
	/***************************
	 * getter and setter method
	 **************************/
	public User getUser() {
		return user;
	}
	public void setUser(User user) {
		this.user = user;
	}
	public Long getId() {
		return id;
	}
	public void setId(Long id) {
		this.id = id;
	}
	
	
}
